package com.ciwong.epaper.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.pad.contentprovider.LoginToken;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.widget.CWDialog;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lzr on 2017/1/4.
 */
public class BaseExtCallBack extends BaseCallBack {

    private Context mContext;
    private String mUserId;
    private  CWDialog dialogExt;

    public boolean isUpload() {
        return isUpload;
    }

    public void setUpload(boolean upload) {
        isUpload = upload;
    }

    //基类是否提交错误日志
    private boolean isUpload =true;


    public BaseExtCallBack(Context mContext,String mUserId){
        this.mContext=mContext;
        this.mUserId=mUserId;
    }

    public BaseExtCallBack(Context mContext){
        this.mContext=mContext;
        if (EApplication.getInstance().getUserInfoBase() != null) {
            this.mUserId=String.valueOf(EApplication.getInstance().getUserInfoBase().getUserId());
        }
    }


    @Override
    public void failed(Object data) {
        if (null != data) {
            FeedbackUtil.getInstance().addFeedbackLog( FeedbackUtil.FEEDBACK_TYPE_REQUEST_ERROR,"失败信息："+data.toString(),"请求服务器报错："+getUrl());
        }

    }

    @Override
    public void failed(int errorCode, Object data) {
        forceBackToAlert(errorCode,data);
    }

    public void forceBackToAlert(int errorCode, Object data){
        if(mContext!=null && mContext instanceof Activity){
            //别人登录了强制退出了
            Activity activity=(Activity)mContext;
            if(activity.isFinishing()){
                return;
            }
            if(errorCode==17){
                try{
                    if(dialogExt==null){
                        dialogExt=new CWDialog(((Activity) mContext));
                    }
                    SimpleDateFormat sd=new SimpleDateFormat("yyyy年MM月dd日");
                    dialogExt.setMessage("你的账号("+mUserId+")于"+sd.format(new Date())+"在另外一台设备登录。如非本人操作，则密码可能已经泄露，建议及时修改密码。");
                    dialogExt.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            loginOut(((Activity) mContext));

                            dialogExt.dismiss();
                        }
                    });
                    dialogExt.show();


                    return;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else if(errorCode==27){
                try{
                    if(dialogExt==null){
                        dialogExt=new CWDialog(((Activity) mContext));
                    }
                    dialogExt.setMessage("用户信息已过期，请重新登录");
                    dialogExt.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (EApplication.PLATFORM_ENV == EApplication.PLATFORM_PAD)
                            {
                                LoginToken.goToUnifiedLogin((Activity)mContext);
                            }else {
                                loginOut(((Activity) mContext));
                            }


                            dialogExt.dismiss();
                        }
                    });
                    dialogExt.show();
                    return;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else if(errorCode==10004){
                //非制作资源商没有权限查看
                try{
                    if(dialogExt==null){
                        dialogExt=new CWDialog(((Activity) mContext));
                    }
                    dialogExt.setMessage(data.toString());
                    dialogExt.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialogExt.dismiss();

                        }
                    });
                    dialogExt.show();
                    return;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else{
                if(null !=data&&isUpload)
                {
                   FeedbackUtil.getInstance().addFeedbackLog( FeedbackUtil.FEEDBACK_TYPE_REQUEST_ERROR,"失败信息："+data.toString()+errorCode,"请求服务器报错:"+getUrl());
                }

//                if(data==null||data.toString().equals("")){
//                    CWToast.makeText(activity, "网络异常", CWToast.LENGTH_LONG).show();
//                }else{
//                    CWToast.makeText(activity, String.valueOf(data),
//                            CWToast.LENGTH_LONG).show();
//                }

            }
        }
    }

    /**
     * 退出方法
     * @param context
     */
    public  void loginOut(Context context){
        LogOutUtil.logOut((Activity) context);
    }

}
